<?php
    header('Content-type:text/json');
    require "../../connection.php";
    try{
            $goods_name = $_POST['goods_name'];
            $goods_price = $_POST['goods_price'];
            $goods_bill_name = $_FILES['file']['name']; // 涉及文件上传
            $goods_label = $_POST['label'];
            $goods_categoryid = $_POST['categoryid'];
            $goods_engname = $_POST['engname'];
            $goods_tomarkettime = $_POST['tomarkettime'];
            $goods_publishtime = $_POST['publishtime'];
            $goods_studio = $_POST['goods_studio'];
            $goods_filesize = $_POST['filesize'];
            $goods_language = $_POST['language'];
            $goods_describe = $_POST['describe'];
            $goods_runsystem = $_POST['runsystem'];
            $goods_buysum = $_POST['buysum'];

            // 获取文件夹名 == 类别名 》 游戏名
            $category_name = $conn->doSql("select en_name from category where id = '".$goods_categoryid."'");
            $spedir_name = substr($goods_bill_name,0, stripos($goods_bill_name,"."));
            // 把需要创建文件夹都放到数组里
            $dirspath = array();
            $orgindir = "../images/goods";
            fzdir($orgindir, $category_name[0]['en_name'],$spedir_name);
            // 生成文件路径
            $filepath = $dirspath[2]."/".$goods_bill_name;
            uploadImg($dirspath,$filepath,$goods_bill_name);
    }catch(Exception $e){
        print_r($e->getMessage());
    }

/***
    dirspath : 多个文件夹数组
    $orgindir： 最内文件夹
    $category_name： 分类文件夹
    $spedir_name： 具体游戏名文件夹
**/
    function fzdir($orgindir,$category_name,$spedir_name){
        global $dirspath;
        $categorydir = $orgindir."/".$category_name;
        $spedir = $categorydir."/".$spedir_name;
        array_push($dirspath,$orgindir,$categorydir,$spedir);

    }

/**
    $dirspath:  多个文件夹数组
    $path: 文件存放位置
    $filename
**/
    function uploadImg($dirspath,$path,$filename){
        global $conn, $goods_label,$goods_describe,$goods_name ,$goods_price ,$goods_categoryid ,$goods_engname ,$goods_tomarkettime ,$goods_publishtime ,$goods_studio ,$goods_filesize ,$goods_language ,$goods_runsystem ,$goods_buysum;
        //创建存在广告的文件夹
        $length = count($dirspath);
        for($i=0;$i<$length;$i++){
            if(!is_dir($dirspath[$i])){
                mkdir($dirspath[$i],0700);
            }
        }
        $content = '';

            // 移动上传的文件到指定目录下
        if(($_FILES['file']['type']=="image/jpeg")
           ||($_FILES['file']['type']=="image/png")
           ||($_FILES['file']['type']=="image/jpg")
        ){
        /*

        错误信息代码。值为0表示没有错误发生，文件上传成功。
        值为1表示上传的文件超过了php.ini文件中upload_max_filesize选项限制的值。
        值为2表示上传文件的大小超过了HTML表单中规定的最大值。
        值为3表示文件只有部分被上传。
        值为4表示没有文件被上传。
        值为5表示上传文件大小为0。
        */
          if($_FILES['file']['error']>0){
            $content.="Return Code: " . $_FILES["file"]["error"] . "<br />";
          }else{
            $content.="Upload: " . $_FILES["file"]["name"] . "<br />";
            $content.="Type: " . $_FILES["file"]["type"] . "<br />";
            $content.="Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
            $content.="Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
            if(file_exists($path)){
                $content.$filename."已经存在";
                $msg = "文件已经存在";
                $data = array("status"=>"1","msg"=>$msg,"content"=>$content);
                echo json_encode($data);
            }else{
                move_uploaded_file($_FILES['file']['tmp_name'],$path);
                // 当文件上传ok时，把数据插入数据库 --存绝对路径
                $abspath = str_replace("..","/admin",$path);
                $arr = array("goods_name"=>$goods_name,"goods_price"=>$goods_price,"goods_bill"=>$abspath,"goods_categoryid"=>$goods_categoryid,
                                   "goods_engname"=>$goods_engname,"goods_tomarkettime"=>$goods_tomarkettime,"goods_publishtime"=>$goods_publishtime,
                                   "goods_studio"=>$goods_studio,"goods_filesize"=>$goods_filesize,"goods_language"=>$goods_language,
                                   "goods_runsystem"=>$goods_runsystem,"goods_buysum"=>$goods_buysum,"goods_label"=>$goods_label,
                                   "goods_describe"=>$goods_describe
                                   );
                $result = $conn->insert("goods",$arr);
                if($result){
                    $msg = "上传成功";
                 }else{
                     $msg = "上传失败";
                 }
                 $data = array("status"=>"1","msg"=>$msg,"content"=>$content);
                 echo json_encode($data);
            }
          }
        }else{
            $content.=  "无效的文件类型";
            $msg = "无效的文件类型";
            $data = array("status"=>"1","msg"=>$msg,"content"=>$content);
            echo json_encode($data);
        }

    }


?>
